//
//  WPBindingPhoneNumVC.m
//  wobingwoyi
//
//  Created by WGP on 16/8/3.
//  Copyright © 2016年 WGP. All rights reserved.
//

#import "WPBindingPhoneNumVC.h"
#import "WLCaptcheButton.h"

@interface WPBindingPhoneNumVC ()

@property (nonatomic,strong)UIView *containtView;
@property (strong, nonatomic)UITextField *phoneNumField;
@property (strong, nonatomic)UITextField *verifyCodeField;
@property (nonatomic,strong)WLCaptcheButton *captcheButton;
@property (strong, nonatomic)UIButton *doneBtn;

@end

@implementation WPBindingPhoneNumVC

- (void)viewDidLoad {
    [super viewDidLoad];
 
    self.title = @"绑定手机号";
    self.view.backgroundColor = GlobalBgColor;
    [self setupSubviews];
    
    self.automaticallyAdjustsScrollViewInsets = YES;
}

-(void)setupSubviews
{
    [self containtView];
    [self phoneNumField];
    [self captcheButton];
    [self verifyCodeField];
    [self doneBtn];
}

-(UIView *)containtView
{
    if (_containtView == nil)
    {
        _containtView = [UIView new];
        _containtView.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:_containtView];
        
        [_containtView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self.view);
            make.height.mas_equalTo(88);
            make.top.equalTo(self.view).offset(64 + 10);
        }];
        
        UIImageView *topLine = [self lineImageView];
        UIImageView *bottomLine = [self lineImageView];
        UIImageView *midLine = [self lineImageView];
        
        [_containtView addSubview:topLine];
        [_containtView addSubview:bottomLine];
        [_containtView addSubview:midLine];
        
        [topLine mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.equalTo(_containtView);
            make.height.mas_equalTo(0.5);
        }];
        
        [bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(_containtView);
            make.height.mas_equalTo(0.5);
        }];
        
        [midLine mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_containtView).offset(15);
            make.right.equalTo(_containtView);
            make.height.mas_equalTo(0.5);
            make.centerY.equalTo(_containtView);
        }];
    }
    return _containtView;
}

-(WLCaptcheButton *)captcheButton
{
    if (_captcheButton == nil)
    {
        _captcheButton = [WLCaptcheButton new];
        _captcheButton.identifyKey = @"WPBindingPhoneNumVC";
        _captcheButton.disabledTitleColor = GloballightFontColor;
        [_captcheButton sizeToFit];
        _captcheButton.titleLabel.font = [UIFont systemFontOfSize:13];
        [_captcheButton setTitleColor:GlobalColor forState:UIControlStateNormal];
//        _captcheButton.backgroundColor = [UIColor orangeColor];
        [_captcheButton addTarget:self action:@selector(getVerifyCodeBtn:) forControlEvents:UIControlEventTouchDown];
        [_captcheButton setTitle:@"获取验证码" forState:UIControlStateNormal];
        [_containtView addSubview:_captcheButton];

        CGSize size = [_captcheButton.titleLabel.text sizeWithMaxSize:CGSizeMake(MAXFLOAT, MAXFLOAT) font:13];
        [_captcheButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(_containtView).offset(-10);
            make.bottom.equalTo(_containtView).offset(1);
            make.height.mas_equalTo(42);
            make.width.mas_equalTo(size.width+1);
        }];
        
        UIImageView *line = [self lineImageView];
        [self.containtView addSubview:line];
        
        [line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(0.5);
            make.right.equalTo(_captcheButton.mas_left).offset(-10);
            make.centerY.equalTo(_captcheButton);
            make.height.mas_equalTo(20);
        }];
    }
    return _captcheButton;
}

-(UITextField *)verifyCodeField
{
    if (_verifyCodeField == nil)
    {
        _verifyCodeField = [UITextField new];
        _verifyCodeField.placeholder = @"请输入验证码";
        _verifyCodeField.clearButtonMode = UITextFieldViewModeWhileEditing;
        _verifyCodeField.font = [UIFont systemFontOfSize:14];
        _verifyCodeField.keyboardType = UIKeyboardTypeNumberPad;
        [self.containtView addSubview:_verifyCodeField];
        
        [_verifyCodeField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.containtView).offset(15);
            make.right.equalTo(self.captcheButton.mas_left).offset(-20);
            make.centerY.equalTo(self.captcheButton);
            make.height.equalTo(self.phoneNumField);
        }];
    }
    return _verifyCodeField;
}

-(UITextField *)phoneNumField
{
    if (_phoneNumField == nil)
    {
        _phoneNumField = [UITextField new];
        _phoneNumField.placeholder = @"请输入手机号码";
        _phoneNumField.clearButtonMode = UITextFieldViewModeWhileEditing;
        _phoneNumField.font = [UIFont systemFontOfSize:14];
        _phoneNumField.keyboardType = UIKeyboardTypeNumberPad;
        [self.containtView addSubview:_phoneNumField];
        
        [_phoneNumField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_containtView).offset(15);
            make.right.equalTo(_containtView).offset(-15);
            make.top.equalTo(_containtView).offset(1);
            make.height.mas_equalTo(42);
        }];
        
    }
    return _phoneNumField;
}

-(UIButton *)doneBtn
{
    if (_doneBtn == nil)
    {
        _doneBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_doneBtn setTitle:@"绑定" forState:UIControlStateNormal];
        [_doneBtn setBackgroundImage:[UIImage imageNamed:@"normal"] forState:UIControlStateNormal];
        [_doneBtn setBackgroundImage:[UIImage imageNamed:@"highLight"] forState:UIControlStateHighlighted];
        [_doneBtn addTarget:self action:@selector(bindingBtnDidClick:) forControlEvents:UIControlEventTouchDown];
        [self.view addSubview:_doneBtn];
        
        [_doneBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.view).offset(20);
            make.right.equalTo(self.view).offset(-20);
            make.height.mas_equalTo(44);
            make.top.equalTo(self.containtView.mas_bottom).offset(80);
        }];
        
    }
    return _doneBtn;
}

- (void)getVerifyCodeBtn:(WLCaptcheButton *)sender
{
    if (![NSString isMobile:self.phoneNumField.text])
    {
        [self showErrorInfoWithStatus:@"手机号码格式不正确"];
        return ;
    }
    
    NSString *url = @"user/sendMessage.do";
    NSDictionary *dict = @{@"telephone":self.phoneNumField.text};
    
    [[WPNetWorkManager sharedNetWorkManager] POST:url parameters:dict progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        if ([responseObject[@"result"] isEqualToString:@"success"])
        {
            [self showSuccessInfoWithStatus:@"验证码已发送"];
            [sender fire];
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        [self showErrorInfoWithStatus:@"网络错误"];
    }];
}

- (void)bindingBtnDidClick:(UIButton *)sender
{
    if (!([NSString isMobile:self.phoneNumField.text] && self.verifyCodeField.text.length))
    {
        [self showErrorInfoWithStatus:@"信息不完整"];
        return ;
    }
    
//    发送验证码
    NSString *url = @"user/bindTel.do";
    
    NSDictionary *dict = @{@"telephone":self.phoneNumField.text,@"codeInput":self.verifyCodeField.text};
    
    [[WPNetWorkManager sharedNetWorkManager] POST:url parameters:dict progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        if ([responseObject[@"result"] isEqualToString:@"success"])
        {
            [self showSuccessInfoWithStatus:@"绑定成功"];
            
            WPUserAccount *userAccount = [WPUserAccountViewModel sharedInstance].userAccount;
            userAccount.user.phone = self.phoneNumField.text;
            [userAccount saveUserAccount];
            
            [self.navigationController popViewControllerAnimated:YES];
        }
        else
        {
            if([responseObject[@"detail"] isEqualToString:@"areadyRegister"])
            {
                [self showErrorInfoWithStatus:@"该手机号已经注册"];
            }
            else
            {
                [self showErrorInfoWithStatus:@"验证码错误"];
            }
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        [self showErrorInfoWithStatus:@"网络错误"];
    }];
}

-(UIImageView *)lineImageView
{
    UIImageView *imageView = [[UIImageView alloc] init];
    imageView.backgroundColor = GLobalLineColor;
    
    return imageView;
}
@end
